package com.zlsy.learning.leetcode.stack.s155;

import java.util.Stack;

/**
 * @author zhouliang
 * @date 2020/8/11 13:56 方法二、Stack<Node>
 *    <p>时间复杂度：O（1） 空间复杂度：O（N）
 */
public class MinStack2 {

  private Stack<Node> stack;

  public MinStack2() {
    stack = new Stack<>();
  }

  public void push(int x) {
    if (stack.isEmpty()) {
      stack.push(new Node(x, x));
    } else {
      stack.push(new Node(x, Math.min(x, stack.peek().min)));
    }
  }

  public void pop() {
    stack.pop();
  }

  public int top() {
    return stack.peek().val;
  }

  public int getMin() {
    return stack.peek().min;
  }

  private static class Node {
    int val;
    int min;

    public Node(int val, int min) {
      this.val = val;
      this.min = min;
    }
  }
}
